﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using MedLink2011.Views.ClinicCharts.ViewModels;
using MedLink2011.Web.Services;

namespace MedLink2011.Views.ClinicCharts.ChildWindows
{
    public partial class ClinicChartsAddCW : ChildWindow
    {
        
        readonly int loadedClinicChart_Id = MedLink2011.Views.ClinicCharts.ViewModels.ClinicChartViewModel.DashLoad_PK;
        //private int loadedPatient_Id = 1;
        private EntityQuery<MedLink2011.Web.Models.Patient> _patientQuery;
        private MedLink2011Context _context;
        
        public ClinicChartsAddCW()
        {
            InitializeComponent();
        LoadComboBoxData();
            
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                ClinicChart_IdTextBox.Text = loadedClinicChart_Id.ToString(); //make sure is not the primary key of the current form, it needs to be a FOREIGN key.
                Patient_IdTextBox.Text = Patient_IdComboBox.SelectedIndex.ToString();
            if (!ClinicChartViewModel._clinicChartContext.IsLoading)
                {
                    this.ClinicChartsDataForm.CommitEdit();
                    this.DialogResult = true;
                }
            }
            
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        
        private void LoadComboBoxData()
        {

            //Patient_IdComboBox
             _context = new MedLink2011Context();
            _patientQuery = _context.GetPatientsQuery();
            _patientQuery = _patientQuery.OrderBy(e => e.Patient_Id);
            _context.Load(_patientQuery, LoadBehavior.RefreshCurrent, Patient_IdComboBox_Loaded, null);
            Patient_IdComboBox.ItemsSource = _context.Patients;
        }


            private void Patient_IdComboBox_Loaded(LoadOperation<MedLink2011.Web.Models.Patient> lo)
            {
                this.Patient_IdComboBox.SelectedIndex = 1;
            }

            private void ClinicChartsDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
            {
                this.Close();
            }

    }
}

